package com.vay.de.dang.online.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.List;

import com.vay.de.dang.online.R;

public class RollTextview extends TextSwitcher implements ViewSwitcher.ViewFactory {

    private static final int FLAG_START_AUTO_SCROLL = 0;
    private static final int FLAG_STOP_AUTO_SCROLL = 1;

    private static final int STATE_PAUSE = 2;
    private static final int STATE_SCROLL = 3;

    private Context context;
    private List<String> list = new ArrayList<>();
    private int mScrollState = STATE_PAUSE;
    private Handler handler = new Handler(Looper.getMainLooper());
    private int currentId = 0;
    private int intervalTime = 3000;
    private LayoutInflater inflater;

    public RollTextview(Context context) {
        this(context, null);
        init(context);
    }

    public RollTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void loadTexts(List<String> str) {
        if (str == null) return;
        currentId = 0;
        list.clear();
        list.addAll(str);
    }

    private void init(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.handler = new Handler(Looper.getMainLooper()) {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case FLAG_START_AUTO_SCROLL:
                        if (list.size() > 0) {
                            currentId++;
                            setText(list.get(currentId % list.size()));
                        }
                        handler.sendEmptyMessageDelayed(FLAG_START_AUTO_SCROLL, intervalTime);
                        break;
                    case FLAG_STOP_AUTO_SCROLL:
                        handler.removeMessages(FLAG_START_AUTO_SCROLL);
                        break;
                }
            }
        };

        setFactory(this);
        int animDuration = 600;
        Animation in = new TranslateAnimation(0, 0, animDuration, 0);
        in.setDuration(animDuration);
        in.setInterpolator(new AccelerateInterpolator());
        Animation out = new TranslateAnimation(0, 0, 0, -animDuration);
        out.setDuration(animDuration);
        out.setInterpolator(new AccelerateInterpolator());
        setInAnimation(in);
        setOutAnimation(out);
    }

    public void startAutoScroll() {
        mScrollState = STATE_SCROLL;
        handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL);
    }

    public void stopAutoScroll() {
        mScrollState = STATE_PAUSE;
        handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL);
    }

    @Override
    public View makeView() {
        return inflater.inflate(R.layout.view_hr_tv, null);
    }

    public boolean isScroll() {
        return mScrollState == STATE_SCROLL;
    }

    public boolean isPause() {
        return mScrollState == STATE_PAUSE;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }

}
